#!/usr/bin/sh
#
# dnlcstat - DNLC statistics.
#            Written in DTrace (Solaris 10 3/05).
#
# The DNLC is the Directory Name Lookup Cache. Filename lookups often
# return a hit from here, before needing to traverse the regular file
# system cache or go to disk.
#
# $Id: dnlcstat 3 2007-08-01 10:50:08Z brendan $
#
# USAGE:	dnlcstat [interval [count]]
#
# FIELDS:
#
#		%hit	hit percentage for this sample
#		hit	number of DNLC hits in this sample
#		miss	number of DNLC misses in this sample
#
# SEE ALSO: 	CacheKit, http://www.brendangregg.com/cachekit.html
#		(contains a dnlcstat written in Perl, which uses less CPU)
#
# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
#
# CDDL HEADER START
#
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
#
#  You can obtain a copy of the license at Docs/cddl1.txt
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing permissions
#  and limitations under the License.
#
# CDDL HEADER END
#
# 27-Mar-2004	Brendan Gregg	Created this.
# 14-Jun-2005	   "      "  	Updated style.
# 14-Jun-2005	   "      "  	Last update.
#

##############################
# --- Process Arguments ---
#

### default values
interval=1; count=-1

### check arguments
if [ "$1" = "-h" -o "$1" = "--help" ]; then
	cat <<-END >&2
	USAGE: dnlcstat [interval [count]]
	       dnlcstat          # 1 second samples, infinite
	  eg,
	       dnlcstat 1        # print every 1 second
	       dnlcstat 5 6      # print every 5 seconds, 6 times
	END
        exit 1
fi

### argument logic
if [ "$1" -gt 0 ]; then
        interval=$1; count=-1; shift
fi
if [ "$1" -gt 0 ]; then
        count=$1; shift
fi
if [ $interval -eq 0 ]; then
        interval=1
fi


#################################
# --- Main Program, DTrace ---
#
/usr/sbin/dtrace -n '
 #pragma D option quiet

 /*
  * Command line arguments
  */
 inline int INTERVAL   = '$interval';
 inline int COUNTER    = '$count';
 inline int SCREEN = 21;

 int hits;			/* hits */
 int misses;			/* misses */

 /*
  * Initialise variables
  */
 dtrace:::BEGIN
 {
	lines = SCREEN + 1;
	counts = COUNTER;
	secs = INTERVAL;
	first = 1;
 }

 /*
  * Print header
  */
 dtrace:::BEGIN,
 tick-1sec
 /first || (secs == 0 && lines > SCREEN)/
 { 
	printf("%10s %8s %8s\n","dnlc  %hit","hit","miss");
	lines = 0;
	first = 0;
 }

 /*
  * Probe DNLC lookups
  */
 fbt:genunix:dnlc_lookup:return
 {
	hits   += arg1 == 0 ? 0 : 1;
	misses += arg1 == 0 ? 1 : 0;
 }

 profile:::tick-1sec
 {
        secs--;
 }


 /*
  * Print output line
  */
 profile:::tick-1sec
 /secs == 0/
 {
	/* calculate hit percent */
	this->divide = misses + hits == 0 ? 1 : misses + hits;
	ratio = hits * 100 / this->divide;

	/* print output */
	printf("%10d %8d %8d\n",ratio,hits,misses);

	/* clear counters */
	hits = 0;
	misses = 0;

        /* process counts */
        secs = INTERVAL;
        counts--;
        lines++;

 }

 /*
  * End
  */
 profile:::tick-1sec
 /counts == 0/
 {
        exit(0);
 }
'

